package com.hfut.eeg.controller;

import com.hfut.eeg.common.api.CommonResult;
import com.hfut.eeg.dto.LoginParam;
import com.hfut.eeg.dto.User;
import com.hfut.eeg.service.EEGService;
import com.hfut.eeg.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.controller
 * @Description: TODO
 * @date 2019-11-05 11:20
 */
@Controller
@Api(tags = "LoginController", value = "登录")
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private EEGService eegService;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    //SpringSecurity 权限管理 JWT token验证
    @ApiOperation("用户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult userLogin(@RequestBody LoginParam param){


        //System.out.println("parm:"+param);


        HashMap<String,Object> result = loginService.userLogin(param);
        if (result == null){
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("token", result.get("token"));
        tokenMap.put("user",result.get("user"));
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }

    @ApiOperation("用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult userRegister(@RequestBody User param){
        //System.out.println(param);
        int result = loginService.userRegister(param);
        return CommonResult.success(result);
    }

}
